# This file is used to compile C NIF module in Elixir buildchain
MIX = mix
CFLAGS = -g -O2 -Wall
#CFLAGS = -g -O3 -ansi -pedantic -Wall -Wextra -Wno-unused-parameter

ERLANG_PATH = $(shell erl -eval 'io:format("~s", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)
ERL_INTERFACE_PATH = $(shell erl -eval 'io:format("~s", [code:lib_dir(erl_interface, lib)])' -s init stop -noshell)
CFLAGS += -I$(ERLANG_PATH)
LDFLAGS += -lexpat

ifneq ($(OS),Windows_NT)
	CFLAGS += -fPIC

	ifeq ($(shell uname),Darwin)
		LDFLAGS += -dynamiclib -undefined dynamic_lookup
	endif

	ifeq ($(shell uname),FreeBSD)
		CFLAGS  += -I/usr/local/include
		LDFLAGS += -L/usr/local/lib
	endif

	ifeq ($(shell uname),Linux)
		LDFLAGS += -shared -L$(ERL_INTERFACE_PATH) -lerl_interface -lei
	endif
endif

.PHONY: all fast_xml clean

all: fast_xml

fast_xml:
	$(MIX) compile

c_src/%.o: c_src/%.c
	$(CC) -c $(CFLAGS) -o $@ $<

priv/lib/%.so:
	@mkdir -p priv/lib
	$(CC) $< $(LDFLAGS) -o $@

priv/lib/fxml_stream.so: c_src/fxml_stream.o

priv/lib/fxml.so: c_src/fxml.o

clean:
	$(RM) priv/lib/fxml_stream.so
	$(RM) priv/lib/fxml.so

# Local Variables:
# mode: makefile
# End:
# vim: set filetype=makefile
